package tech.aistar.controller;

import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import tech.aistar.entity.Customer;
import tech.aistar.service.ICustomerService;

import javax.annotation.Resource;

@Controller
public class LoginAndRegisterController {
    //注册，向数据库添加一条数据
    @Autowired
    @Resource
    private ICustomerService customerService;

    @PostMapping("register")
    public ModelAndView addCustomer(String custName, String custPwd, String custAge) {
        Customer customer = new Customer();
        customer.setCustName(custName);
        customer.setCustPwd(custPwd);
        customer.setCustAge(custAge);
        customerService.save(customer);
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }

    //登录
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String Login(String custName, String custPwd) {
        Customer customer = customerService.getInfo(custName, custPwd);
        if (customer != null) {
            return "redirect:index.jsp";
        } else {
            return "redirect:login.jsp";
        }
    }

}


